using System;

namespace MiniFlower
{
    public abstract class ControllerBase
    {
        protected CharacterBase m_Character;

        public CharacterBase Character => m_Character;

        public ControllerBase() { }

        public void Possess(CharacterBase character)
        {
            if (character == null)
            {
                throw new ArgumentNullException(nameof(character));
            }

            if (m_Character != null)
            {
                UnPossess();
            }
            m_Character?.Controller?.UnPossess();

            OnPossess(character);
        }

        public void UnPossess()
        {
            OnUnPossess();
        }

        protected virtual void OnPossess(CharacterBase character)
        {
            character.PossessedBy(this);

            m_Character = character;
        }

        protected virtual void OnUnPossess()
        {
            if (m_Character != null)
            {
                m_Character.UnPossessed();
                m_Character = null;
            }
        }
    }

}
